﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.controls.Tree;
using umbraco.uicontrols;
using umbraco;
using umbraco.cms.presentation.Trees;
using umbraco.BasePages;
using umbraco.cms.businesslogic.datatype;
using TheOutfield.UmbExt.UniversalMediaPicker.Interfaces;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Dialogs
{
    public partial class UniversalMediaPicker : UmbracoEnsuredPage
    {
        private ICreateControl createControl;

        public int DataTypeDefinitionID
        {
            get
            {
                return Convert.ToInt32(Request.QueryString["dtdid"]);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            // Create tabs
            TabPage page = tv_options.NewTabPage(ui.Text("choose"));
            page.HasMenu = false;
            page.Controls.Add(pane_select);

            // Only show tabe if we have a create control
            var provider = UniversalMediaPickerProviderFactory.GetProvider(DataTypeDefinitionID);

            try
            {
                if (provider.CreateControl != null)
                {
                    pane_upload.Visible = true;

                    TabPage page2 = tv_options.NewTabPage(ui.Text("create") + " " + ui.Text("new"));
                    page2.HasMenu = false;
                    page2.Controls.Add(pane_upload);

                    // Create form
                    createControl = provider.CreateControl;
                    createControl.Control.ID = "UniversalMediaPicker_" + provider.Alias + "CreateControl";
                    //createControl.Control.EnableViewState = false;

                    phCreateForm.Controls.Add(createControl.Control);
                }
            }
            catch (NotImplementedException)
            { }

            base.OnInit(e);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            EnsureChildControls();

            MediaItem savedMediaItem;
            string message;

            if (createControl.TrySave(out savedMediaItem, out message))
            {
                UniversalMediaPickerCache.Flush(DataTypeDefinitionID + "$");

                if (!string.IsNullOrEmpty(message))
                {
                    // Show success message
                    feedback.Style.Add("margin-bottom", "10px");
                    feedback.type = Feedback.feedbacktype.success;
                    feedback.Text = message;
                }

                if (savedMediaItem != null && !string.IsNullOrEmpty(savedMediaItem.Id))
                {
                    // Call JS method
                    feedback.Text += @"
                        <script type='text/javascript'>
                            jQuery(document).ready(function() {
                                uploadHandler.call(this, " + savedMediaItem.ToBasicMediaItem().SerializeToJson().Replace("'", "\'").Replace("\"", "\'") + @");
                            });
                        </script>
                    ";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(message))
                {
                    // Show error message
                    feedback.Style.Add("margin-bottom", "10px");
                    feedback.type = Feedback.feedbacktype.error;
                    feedback.Text = message;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptResource(this, typeof(UniversalMediaPickerDataType), "TheOutfield.UmbExt.UniversalMediaPicker.Scripts.JSON.js");

            this.DataBind();
        }

    }
}